package com.zsc.common.web.util;

import com.zsc.common.web.result.Result;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by xbkaishui on 17/3/21.
 */
public class ResponseUtils {

    private static final Logger logger = LoggerFactory.getLogger(ResponseUtils.class);

    public static void fail(HttpServletResponse response, CodeEnum code) {
        fail(response, code.getCode(), code.getDesc());
    }

    public static void fail(HttpServletResponse response, Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        String content = JsonUtils.toJson(result);
        try {
            response.setCharacterEncoding(Constant.DEFAULT_CHARSET_NAME);
            response.getWriter().write(content);
            response.flushBuffer();
        } catch (IOException e) {
            logger.error("flush seesion msg error!", e);
        }
    }


}
